package com.itetchologyandenglish.home.util;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.itetchologyandenglish.home.enetity.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/util")
public class UtilController {
	@GetMapping("/createCode")
	public void createCode(HttpSession session,
				HttpServletResponse response){
		try {
			OutputStream outputStream = response.getOutputStream();
			int w = 100, h = 46;
			String code = VerifyCodeUtils.generateVerifyCode(4);
			System.out.println(session.getId()+"-----------"+code);
			session.setAttribute("code", code);
			VerifyCodeUtils.outputImage(w, h, outputStream, code);
			System.out.println(session.getId()+"-----------"+code);
			
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@ResponseBody
	@GetMapping("/verifyCode")
	public Result<Object> verifyCode(String code, HttpSession session){
		String sessionCode = (String) session.getAttribute("code");
		System.out.println("-----session:"+session.getId());
		System.out.println("-----sessionCode"+sessionCode);
		if (code.equalsIgnoreCase((String) session.getAttribute("code"))) {
			return new Result<>(200, "success");
		}
		return new Result<>(200, "error");
	}
}
